﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace D3crft.Windows {

    public partial class wMain : Window {




        public event EventHandler evAddStatClicked;
        public event EventHandler evEditStatClicked;
        public event EventHandler evItemStatsChanged;
        public event EventHandler evItemEquippedChanged;

        // character chooser
        public event Characters.ClassObjects.ocCharacter.CharacterEventHandler evNewCharacterClicked;
        public event Characters.ClassObjects.ocCharacter.CharacterEventHandler evCharacterSelectionChanged;
        public event Characters.ClassObjects.ocCharacter.CharacterEventHandler evCharacterDeleted;

        // character editor
        public event EventHandler evCharacterStatChanged;

        // item editor
        public event UserControls.ucItemEditor.delAddStatHandler evItems_ItemEditor_AddClicked;
        public event UserControls.ucItemEditor.delEditStatHandler evItems_ItemEditor_StatClicked;
        public event UserControls.ucItemEditor.ItemDPSHandler evItems_ItemEditor_ItemStatsChanged;
        public event Characters.ClassObjects.ocCharacter.CharacterEventHandler evItems_ItemEditor_NonItemizedCharacterChanged;

        Effects.ecShadowBlurQueue oEffect;

        // ####################################################
        // #####    Object Initialization
        // ####################################################
        #region Object Initialization

        public wMain() {
            do_Initialize();
            do_Preset();
            do_Startup();
        }

        ~wMain() {
            do_Finalize();
        }

        private void do_Initialize() {
            InitializeComponent();
        }

        private void do_Preset() {
            // general
            this.Loaded += new RoutedEventHandler(ev_Object_Loaded);
            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(ev_Object_VisibleChanged);
            oEffect = new Effects.ecShadowBlurQueue(RootGrid);
            oEffect.MaxOpacity = 0.24;

            // tab control
            TabControlAdvanced.evTabSelectionChanged += new EventHandler(ev_General_TabControlAdvanced_TabSelectionChanged);

            // character chooser
            Characters_CharacterChooser.evNewCharacterPressed += new Characters.ClassObjects.ocCharacter.CharacterEventHandler(ev_Characters_CharacterChooser_NewCharacterPressed);
            Characters_CharacterChooser.evCharacterSelected += new Characters.ClassObjects.ocCharacter.CharacterEventHandler(ev_Characters_CharacterChooser_CharacterSelected);
            Characters_CharacterChooser.evCharacterDeleted += new Characters.ClassObjects.ocCharacter.CharacterEventHandler(ev_Characters_CharacterChooser_CharacterDeleted);

            // character editor
            Characters_CharacterEditor.evStatChanged += ev_Characters_CharacterEditor_StatChanged;
            
            //item editors
            Items_ItemEditorOne.evAddClicked += new UserControls.ucItemEditor.delAddStatHandler(ev_Items_ItemEditor_AddClicked);
            Items_ItemEditorOne.evStatClicked += new UserControls.ucItemEditor.delEditStatHandler(ev_Items_ItemEditor_StatClicked);
            Items_ItemEditorOne.evItemStatsChanged += new UserControls.ucItemEditor.ItemDPSHandler(ev_Items_ItemEditor_ItemStatsChanged);
            Items_ItemEditorOne.evNonItemizedCharacterChanged += new Characters.ClassObjects.ocCharacter.CharacterEventHandler(ev_Items_ItemEditor_NonItemizedCharacterChanged);
            Items_ItemEditorTwo.evAddClicked += new UserControls.ucItemEditor.delAddStatHandler(ev_Items_ItemEditor_AddClicked);
            Items_ItemEditorTwo.evStatClicked += new UserControls.ucItemEditor.delEditStatHandler(ev_Items_ItemEditor_StatClicked);
            Items_ItemEditorTwo.evItemStatsChanged += new UserControls.ucItemEditor.ItemDPSHandler(ev_Items_ItemEditor_ItemStatsChanged);
            Items_ItemEditorTwo.evNonItemizedCharacterChanged += new Characters.ClassObjects.ocCharacter.CharacterEventHandler(ev_Items_ItemEditor_NonItemizedCharacterChanged);
        }

        private void do_Startup() {
            
        }

        private void do_PostStartup() {
            (FindName("Tab_Characters") as TabItem).IsSelected = true;

            Items_ItemEditorOne.SetEquipped(true);
            Items_ItemEditorTwo.SetEquipped(false);
            Items_ItemComparison.SetEquippedItem(1);
        }

        private void do_Finalize() {

        }

        #endregion

        // ####################################################
        // #####    Public Access
        // ####################################################
        #region Public Access

        public void AddCharacterToList(Characters.ClassObjects.ocCharacter oCharacter) {
            Characters_CharacterChooser.AddCharacter(oCharacter.sID, oCharacter.sName, oCharacter.eClass);
        }

        public void RemoveCharacterFromList(string sCharID) {
            Characters_CharacterChooser.RemoveCharacter(sCharID);
        }

        public void ClearCharacterList() {
            Characters_CharacterChooser.ClearCharacters();
        }

        public void SelectCharacter(Characters.ClassObjects.ocCharacter oCharacter) {
            Characters_CharacterChooser.SelectCharacter(oCharacter.sID);
        }

        public void UpdateSelectedCharacter(Characters.ClassObjects.ocCharacter oCharacter) {
            Characters_CharacterEditor.SetEditingCharacter(oCharacter);
            Items_ItemComparison.SetCharacter(oCharacter);
            Items_ItemEditorOne.SetCharacter(oCharacter);
            Items_ItemEditorTwo.SetCharacter(oCharacter);
            UpdateSelectedCharacterStats(oCharacter);
        }

        public void UpdateSelectedCharacterStats(Characters.ClassObjects.ocCharacter oCharacter) {
            UpdateDPSPreview(oCharacter.sName, (Analysis.scCharacterAnalysis.GetCharacterDPS(oCharacter) / 1000.0f).ToString("N1", CultureInfo.InvariantCulture).Replace(',', '.') + "k");
            Items_ItemEditorOne.ForceDPSRecalculation();
            Items_ItemEditorTwo.ForceDPSRecalculation();
            // item editors force UpdateItemComparisonDPS() when a change is detected
        }

        public void UpdateItemEditorNonItemizedCharacter(object oSender, Characters.ClassObjects.ocCharacter oCharacter) {
            if (oSender.Equals(Items_ItemEditorOne)) {
                Items_ItemEditorTwo.SetNonItemizedCharacter(oCharacter);
            }
            else if (oSender.Equals(Items_ItemEditorTwo)) {
                Items_ItemEditorOne.SetNonItemizedCharacter(oCharacter);
            }
        }

        public void UpdateItemComparisonDPS(object oSender, float fBaseDPS, float fItemDPS) {
            if (oSender == null) {
                Items_ItemComparison.SetItemOneDPS(fBaseDPS, fItemDPS);
                Items_ItemComparison.SetItemTwoDPS(fBaseDPS, fItemDPS);
            }
            else if (oSender.Equals(this.Items_ItemEditorOne)) {
                Items_ItemComparison.SetItemOneDPS(fBaseDPS, fItemDPS);
            }
            else if (oSender.Equals(this.Items_ItemEditorTwo)) {
                Items_ItemComparison.SetItemTwoDPS(fBaseDPS, fItemDPS);
            }
        }

        public void QueueShadowOverlay(int iQueueChange) {
            oEffect.Queue(iQueueChange);
        }

        public void UpdateDPSPreview(string sCharName, string sCharDPS) {
            Global_DPSPreview.UpdateInfo(sCharName, sCharDPS);
        }

        #endregion

        // ####################################################
        // #####    Private Functions
        // ####################################################
        #region Private Functions

        #endregion

        // ####################################################
        // #####    Events
        // ####################################################
        #region Events

        private void ev_Object_Loaded(object sender, EventArgs ea) {
            do_PostStartup();
        }

        private void ev_Object_VisibleChanged(object oSender, DependencyPropertyChangedEventArgs ea) {
            if (this.IsVisible) {

            }
        }

        private void ev_Titlebar_MouseDown(object oSender, MouseButtonEventArgs ea) {
            if (oSender == Titlebar_ButtonMinimize) {
                ev_Titlebar_ButtonMinimize_MouseUp(oSender, ea);
            }
            else if (oSender == Titlebar_ButtonClose) {
                ev_Titlebar_ButtonClose_MouseUp(oSender, ea);
            }
            else {
                if (ea.LeftButton == MouseButtonState.Pressed) {
                    this.DragMove();
                    if (ea.ClickCount > 1) {
                        // double click
                        ev_Titlebar_MouseDoubleClick(oSender, ea);
                    }
                    else {
                        Titlebar_Grid.RaiseEvent(new MouseButtonEventArgs((MouseDevice)ea.Device, ea.Timestamp, MouseButton.Left) {
                            RoutedEvent = Mouse.MouseUpEvent,
                            Source = oSender,
                        });
                    }
                }
            }
        }

        private void ev_Titlebar_MouseUp(object oSender, MouseButtonEventArgs ea) {

        }

        private void ev_Titlebar_MouseDoubleClick(object oSender, MouseButtonEventArgs ea) {
            if (!ea.Handled) {
                if (this.WindowState == System.Windows.WindowState.Normal) {
                    this.WindowState = System.Windows.WindowState.Maximized;
                }
                else {
                    this.WindowState = System.Windows.WindowState.Normal;
                }
            }
        }

        private void ev_Titlebar_ButtonMinimize_MouseUp(object oSender, MouseButtonEventArgs ea) {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void ev_Titlebar_ButtonClose_MouseUp(object oSender, MouseButtonEventArgs ea) {
            Environment.Exit(0);
        }

        private void ev_General_TabControlAdvanced_TabSelectionChanged(object oSender, EventArgs ea) {
            if ((oSender as UserControls.ucTabControlAdvanced).get_SelectedTab() == UserControls.ucTabControlAdvanced.eTab.Options) {
                Tab_Options.IsSelected = true;
            }
            else if ((oSender as UserControls.ucTabControlAdvanced).get_SelectedTab() == UserControls.ucTabControlAdvanced.eTab.Characters) {
                Tab_Characters.IsSelected = true;
            }
            else if ((oSender as UserControls.ucTabControlAdvanced).get_SelectedTab() == UserControls.ucTabControlAdvanced.eTab.Item_Comparison) {
                Tab_ItemComparison.IsSelected = true;
            }
            else if ((oSender as UserControls.ucTabControlAdvanced).get_SelectedTab() == UserControls.ucTabControlAdvanced.eTab.Simulation) {
                Tab_Simulation.IsSelected = true;
            }
        }

        private void ev_Characters_CharacterChooser_NewCharacterPressed(object oSender, Characters.ClassObjects.CharacterEventArgs oCharacterEventArgs) {
            if (evNewCharacterClicked != null) evNewCharacterClicked(oSender, oCharacterEventArgs);
        }

        private void ev_Characters_CharacterChooser_CharacterSelected(object oSender, Characters.ClassObjects.CharacterEventArgs oCharacterEventArgs) {
            if (evCharacterSelectionChanged != null) evCharacterSelectionChanged(oSender, oCharacterEventArgs);
        }

        private void ev_Characters_CharacterChooser_CharacterDeleted(object oSender, Characters.ClassObjects.CharacterEventArgs oCharacterEventArgs) {
            if (evCharacterDeleted != null) evCharacterDeleted(oSender, oCharacterEventArgs);
        }

        private void ev_Characters_CharacterEditor_StatChanged(object oSender, EventArgs ea) {
            if (evCharacterStatChanged != null) evCharacterStatChanged(oSender, ea);
        }

        private void ev_Items_ItemEditor_AddClicked(object oSender, UserControls.ucItemEditor.delStatReturnHandler oReturnHandler) {
            if (evItems_ItemEditor_AddClicked != null) evItems_ItemEditor_AddClicked(oSender, oReturnHandler);
        }

        private void ev_Items_ItemEditor_StatClicked(object oSender, int iStatID, float fStatAmmount, int iStatIndex, UserControls.ucItemEditor.delStatReturnHandler oReturnHandler) {
            if (evItems_ItemEditor_StatClicked != null) evItems_ItemEditor_StatClicked(oSender, iStatID, fStatAmmount, iStatIndex, oReturnHandler);
        }

        private void ev_Items_ItemEditor_ItemStatsChanged(object oSender, UserControls.ItemDPSEventArgs eaItemDPS) {
            if (evItems_ItemEditor_ItemStatsChanged != null) evItems_ItemEditor_ItemStatsChanged(oSender, eaItemDPS);
        }

        private void ev_Items_ItemEditor_NonItemizedCharacterChanged(object oSender, Characters.ClassObjects.CharacterEventArgs eaCharacter) {
            if (evItems_ItemEditor_NonItemizedCharacterChanged != null) evItems_ItemEditor_NonItemizedCharacterChanged(oSender, eaCharacter);
        }

        #endregion

    }
}
